package com.hschinese.pronunciation.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.adapter.MsgFlowAdapter;
import com.hschinese.pronunciation.bean.AppDataBean;
import com.hschinese.pronunciation.bean.ListViewItem;
import com.hschinese.pronunciation.db.DBHelper;
import com.hschinese.pronunciation.utils.Logger;
import com.hschinese.pronunciation.widget.Header;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wo
 * 列表页
 */
public class WordListActivity extends BaseActivity {
    private FragmentManager mFragmentManager;
    private static Context mContext = null;
    private static int lessonType = 4;
    public static WordListActivity sInstance = null;
    private Header mHeader = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_words_list);
        sInstance = WordListActivity.this;
        mFragmentManager = getSupportFragmentManager();
        lessonType = getIntent().getIntExtra("lessonType", 4);
        mContext = WordListActivity.this;
        mHeader = (Header)findViewById(R.id.words_list_header);
        if (lessonType == 4) {
            mHeader.setTitle(getResources().getString(R.string.vowel_list_title));
        } else {
            mHeader.setTitle(getResources().getString(R.string.consonants_list_title));
        }
        mHeader.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (savedInstanceState == null) {
            mFragmentManager.beginTransaction()
                    .add(R.id.container, new MsgFlowFragment()).commit();
        }
    }

    @Override
    protected void onDestroy() {
        finish();
        super.onDestroy();
    }

    /**
     * 消息流模拟界面
     */
    public static class MsgFlowFragment extends Fragment {
        private Cursor cursor;
        private List<AppDataBean> mDataList;
        private ListView mListView;
        private int currentPosition = 0;

        public MsgFlowFragment() { }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.word_fragment_main, container,  false);
            mListView = (ListView) rootView.findViewById(R.id.words_listview);
            // 设置Adapter
            mListView.setAdapter(new MsgFlowAdapter(getActivity(), initData()));
            // 点击事件
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                }
            });
            return rootView;
        }

        private List<ListViewItem> initData() {
            if (lessonType == 4) return initVowelData();
            else return initConsonatData();
        }

        private List<ListViewItem> initVowelData() {
            List<ListViewItem> items = new ArrayList<ListViewItem>();
            ListViewItem singleVowelItem = new ListViewItem();
            singleVowelItem.mText = getResources().getString(R.string.single_vowel);
            ListViewItem doubleVowelItem = new ListViewItem();
            doubleVowelItem.mText = getResources().getString(R.string.double_vowel);
            ListViewItem threeTimesVowelItem = new ListViewItem();
            threeTimesVowelItem.mText = getResources().getString(R.string.three_times_vowel);
            mDataList = new ArrayList<AppDataBean>();
            cursor = DBHelper.getInstance(getActivity()).query(DBHelper.RES_TABLE, " type=? ", new String[]{lessonType + ""});
            if (cursor != null)
                Logger.e("xionghy-cursor: " + cursor.getCount());
            while(cursor.moveToNext()) {
                AppDataBean d = new AppDataBean();
                d.setChinese(cursor.getString(cursor.getColumnIndex("chinese")));
                d.setPinyin(cursor.getString(cursor.getColumnIndex("pinyin")));
                d.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
                d.setTone(cursor.getString(cursor.getColumnIndex("tone")));
                d.setAudioName(cursor.getString(cursor.getColumnIndex("audioName")));
                d.setPicName(cursor.getString(cursor.getColumnIndex("picName")));
                d.setType(cursor.getInt(cursor.getColumnIndex("type")));
                d.setGroupID(cursor.getInt(cursor.getColumnIndex("groupId")));
                d.setWeight(cursor.getInt(cursor.getColumnIndex("weight")));
                if (d.getGroupID() == 1) {
                    singleVowelItem.mWords.add(d);
                } else if (d.getGroupID() == 2) {
                    doubleVowelItem.mWords.add(d);
                } else if (d.getGroupID() == 3) {
                    threeTimesVowelItem.mWords.add(d);
                }
            }
            items.add(singleVowelItem);
            items.add(doubleVowelItem);
            items.add(threeTimesVowelItem);
            cursor.close();
            return items;
        }

        private List<ListViewItem> initConsonatData() {
            List<ListViewItem> items = new ArrayList<ListViewItem>();
            ListViewItem labialItem = new ListViewItem();
            labialItem.mText = getResources().getString(R.string.consonants_labial);
            ListViewItem alveolarItem = new ListViewItem();
            alveolarItem.mText = getResources().getString(R.string.consonants_alveolar);
            ListViewItem shegenyinItem = new ListViewItem();
            shegenyinItem.mText = getResources().getString(R.string.consonants_shegenyin);
            ListViewItem tongueToneItem = new ListViewItem();
            tongueToneItem.mText = getResources().getString(R.string.consonants_tongue_tone);
            ListViewItem bTongueToneItem = new ListViewItem();
            bTongueToneItem.mText = getResources().getString(R.string.consonants_before_the_tone_tongue);
            ListViewItem aTongueToneItem = new ListViewItem();
            aTongueToneItem.mText = getResources().getString(R.string.consonants_after_the_tone_tongue);
            cursor = DBHelper.getInstance(getActivity()).query(DBHelper.RES_TABLE, " type=? ", new String[]{lessonType + ""});
            if (cursor != null)
                Logger.e("xionghy-cursor: " + cursor.getCount());
            while(cursor.moveToNext()) {
                AppDataBean d = new AppDataBean();
                d.setChinese(cursor.getString(cursor.getColumnIndex("chinese")));
                d.setPinyin(cursor.getString(cursor.getColumnIndex("pinyin")));
                d.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
                d.setTone(cursor.getString(cursor.getColumnIndex("tone")));
                d.setAudioName(cursor.getString(cursor.getColumnIndex("audioName")));
                d.setPicName(cursor.getString(cursor.getColumnIndex("picName")));
                d.setType(cursor.getInt(cursor.getColumnIndex("type")));
                d.setGroupID(cursor.getInt(cursor.getColumnIndex("groupId")));
                d.setWeight(cursor.getInt(cursor.getColumnIndex("weight")));
                if (d.getGroupID() == 1) {
                    labialItem.mWords.add(d);
                } else if (d.getGroupID() == 2) {
                    alveolarItem.mWords.add(d);
                } else if (d.getGroupID() == 3) {
                    shegenyinItem.mWords.add(d);
                } else if (d.getGroupID() == 4) {
                    tongueToneItem.mWords.add(d);
                } else if (d.getGroupID() == 5) {
                    aTongueToneItem.mWords.add(d);
                } else if (d.getGroupID() == 6) {
                    bTongueToneItem.mWords.add(d);
                }
            }
            items.add(labialItem);
            items.add(alveolarItem);
            items.add(shegenyinItem);
            items.add(tongueToneItem);
            items.add(aTongueToneItem);
            items.add(bTongueToneItem);
            cursor.close();
            return items;
        }
    }
}
